import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
// import 'element-ui/lib/theme-default/index.css'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'模拟数据注释掉
// Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'
//引入axios
import axios from 'axios'

import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
    // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
    ak: 'PGynARu7Fy8xjwn3R9gPIBVdIWTu6iFY'
})
//每次发送请求，会添加此前缀
axios.defaults.baseURL='http://localhost:80'
//配置变量
Vue.prototype.$http=axios

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})

axios.interceptors.request.use(config => {
    if (sessionStorage.getItem('token')) {
        // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        config.headers['X-TOKEN'] = sessionStorage.getItem('token')
    }
    console.debug('config',config)
    return config
}, error => {
    // Do something with request error
    Promise.reject(error)
})

router.beforeEach((to, from, next) => {
    if (to.path == '/login1') {
        sessionStorage.removeItem('token');
    }
    //如果session没有token信息，跳转到登陆页面
    let token = sessionStorage.getItem('token');
    //获得请求栏上的地址
    let url =  window.location.href;
    //获得后台传过来的openid的值（获取http://localhost:8080/#/echarts=?tokenid如这里的tokenid）
    let openid = url.split("&")[1]; //xxx
    //判断是否放行
    if (!token && to.path != '/login1') {
        //判断是否是后台穿过来的，放行注册页面
        if(openid && to.path == '/binder'){
            next();
        }
        //放行我们已经注册了的用户登陆问题
        else if(openid && to.path == '/user'){
            sessionStorage.setItem("token",openid);
            next();
        }
        else {
            next({ path: '/login1' })
        }
    } else {
        next()
    }
})
/*
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login1') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user'));
  if (!user && to.path != '/login1') {
    // next({ path: '/login1' })
      if (!user && to.path == '/user') {
          //获取后台传过来的微信用户openid
          var url =  window.location.href;
          console.debug("地址："+url);
          //以等号分割 获取地址中的用户信息
          var user1 = url.split("&")[1];
          var tokenid = url.split("tokenid")[1];
          sessionStorage.setItem('user', JSON.stringify(user1));
          //并且将当前用户的sessionID保存到浏览器中
          sessionStorage.setItem('token', tokenid);
          //把值存放到前台后就执行下一个操作
          next()
      }else if (!user && to.path == '/binder' ){
          next()
      }else {
          next({ path: '/login1' })
      }
  } else {
    next()
  }
})
*/

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

